package customerEnter.Frame.JDBCDao.JdbcTemplate;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.sql.DataSource;

import customerEnter.Frame.Enum.DataSourceControllerEnum;
import customerEnter.Frame.Utils.Log4j;
import customerEnter.tool.WhetherToEmpty;

public class RandomDataSource implements DataSource {

	private List<DataSource>				dataSourcePool;

	private Map<String, DataSource>			dataMapPool;

	private static ThreadLocal<DataSource>	dsHolder	= new ThreadLocal<DataSource>();

	private static DataSource				dataSource;

	public static String					DataSourceController;

	public RandomDataSource() {
		Log4j.setLog4j(getClass());
	}

	/**
	 * 默認為數據源1
	 * 
	 * 
	 */
	private DataSource randomDs() {
		if (WhetherToEmpty.ObjectWhetherToEmpty(dataSource)) {
			DataSourceController = DataSourceControllerEnum.dataSource_read.getControllerValue();
		}
		dataSource = dataMapPool.get(DataSourceController);
		dsHolder.set(dataSource);
		return dsHolder.get();
	}

	@Override
	public Connection getConnection() throws SQLException {
		Connection conn = randomDs().getConnection();
		return conn;
	}

	public Map<String, DataSource> getDataMapPool() {
		return dataMapPool;
	}

	public void setDataMapPool(Map<String, DataSource> dataMapPool) {
		this.dataMapPool = dataMapPool;
	}

	@Override
	public Connection getConnection(String username, String password) throws SQLException {
		Connection conn = randomDs().getConnection(username, password);
		return conn;
	}

	@Override
	public PrintWriter getLogWriter() throws SQLException {
		return dsHolder.get().getLogWriter();
	}

	@Override
	public int getLoginTimeout() throws SQLException {
		return dsHolder.get().getLoginTimeout();
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		dsHolder.get().setLogWriter(out);
	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		dsHolder.get().setLoginTimeout(seconds);
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return dsHolder.get().isWrapperFor(iface);
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		return dsHolder.get().unwrap(iface);
	}

	public List<DataSource> getDataSourcePool() {
		return dataSourcePool;
	}

	public void setDataSourcePool(List<DataSource> dataSourcePool) {
		this.dataSourcePool = dataSourcePool;
	}

	@Override
	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		return null;
	}

	public static void setDataSourceController(DataSourceControllerEnum dataSourceControllerEnum) {
		DataSourceController = dataSourceControllerEnum.getControllerValue();
		DynamicDataSourceHolder.setDataSource(DataSourceController);
	}

	public static String getDataSourceController() {
		return DataSourceController;
	}

}
